package com.zztl.infiniteBand.ui.activity

import android.app.Activity
import android.content.Intent
import butterknife.OnClick
import com.zztl.infiniteBand.R
import com.zztl.infiniteBand.base.BaseMvpActivity
import com.zztl.infiniteBand.eventbus.ChangeNickNameEvent
import com.zztl.infiniteBand.model.PersonInfoBean
import com.zztl.infiniteBand.model.UploadFileBean
import com.zztl.infiniteBand.net.imp.RequestKeys
import com.zztl.infiniteBand.presenter.PersenInfoPresenter
import com.zztl.infiniteBand.ui.view.mvpview.PersonInfoView
import com.zztl.infiniteBand.util.toast
import kotlinx.android.synthetic.main.activity_set_nick_name.*
import org.greenrobot.eventbus.EventBus

class SetNickNameActivity : BaseMvpActivity<PersonInfoView,PersenInfoPresenter>(), PersonInfoView {
  /*  @BindView(R.id.et_name)
    lateinit var et_name: EditText*/

    lateinit var mInfoPresenter: PersenInfoPresenter
    private var mNickName: String? = null

    override fun setLayoutId(): Int= R.layout.activity_set_nick_name


    override fun initBase() {
        setTitle(getString(R.string.设置昵称))
        setRightTvViewVisible(true)
        val nickname = intent.getStringExtra("nickname")
        if (null != nickname) {
            et_name.setText(nickname)
        }
    }

    override fun createPresenter(): PersenInfoPresenter {
        mInfoPresenter = PersenInfoPresenter()
        return mInfoPresenter
    }


    override fun clickRightTv() {
        mNickName = et_name.text.toString().trim { it <= ' ' }
        if (mNickName!!.isEmpty()) {
//            ToastUtils.show(this, getString(R.string.请输入名字))
            toast(getString(R.string.请输入名字))
        } else {
            mInfoPresenter!!.updatePersonInfo(RequestKeys.NICKNAME, mNickName!!, this)
        }
    }

    @OnClick(R.id.btn_delete)
    fun click() {
        et_name.setText("")
    }

    override fun getInfoSuccess(bean: PersonInfoBean) {

    }

    override fun loadSuccess(bean: UploadFileBean) {

    }

    override fun upDateSuccess(b: Boolean) {
        if (b) {
            if (null != mNickName) {
                val intent = Intent()
                intent.putExtra("name", mNickName)
                setResult(Activity.RESULT_OK, intent)
                EventBus.getDefault().post(ChangeNickNameEvent(mNickName!!))
                finish()
            }
        }
    }

    override fun loadFail() {

    }
}
